package com.example.testlearn;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Spinner spName;
    private ListView lvName;
    private List<String> names;
    //声明ArrayAdapter的适配器对象
    private ArrayAdapter nameAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        //获取界面控件
        getViews();

        //初始化数据源
        initDate();
        //实例化ArrayAdapter的对象
        
        //给listView绑定适配器

        //注册监听器
        setListener();


    }

    private void initDate() {
        //初始化用于显示姓名的listview的数据源
        names = new ArrayList<>();
        //names中添加元素
        names.add("杰克");
        names.add("露丝");
        names.add("李雷");
        names.add("李伟伟");
    }


    private void getViews() {
        //获取下拉列表框
        spName = findViewById(R.id.sp_name);
        //获取用于显示姓名的listView控件
        lvName = findViewById(R.id.lv_name);
    }

    private void setListener() {
        //给下拉列表框注册Item选择事件监听器
        spName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.i("MainAbility", spName.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }






}






















